{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用辗转相除法求最大公约数，因为a，b如果有某个最大公约数c，该公约数c一定是b和a%b的最大公约数，\n",
    "m*c=a\n",
    "n*c=b\n",
    "b%a=(b-s*a)=(n*c-s*m*c)=(n*c-t*c)=x*c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gcd(a, b):\n",
    "    a, b = b, a%b\n",
    "    if b == 0:\n",
    "        return a\n",
    "    return gcd(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "a,b = 18, 12\n",
    "a,b = (a,b) if a > b else (b,a)\n",
    "gcd_ab = gcd(18,12)\n",
    "print(gcd_ab)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
